package tk.mystudio.ocr;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.WritableRaster;
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import javax.imageio.ImageIO;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CopyOfOCR {
    protected transient final static Logger logger = LoggerFactory.getLogger("OCR");
    private final static String LANG_OPTION = "-l";
    private final static String IMAGE_EXT = "tif";
    // private static long PIC_NUM = 0L;
    private final static String EOL = System.getProperty("line.separator");
    private static String tessPath = new File("tesseract").getAbsolutePath();

    public static String read(byte[] image) {
        File file = new File("tesseract", System.currentTimeMillis() + ".png");
        // File file = new File("code", (PIC_NUM++) + "");
        String randCode = "";
        try {
            DataOutputStream dos = new DataOutputStream(new FileOutputStream(file));
            dos.write(image);
            dos.flush();
            dos.close();
            randCode = read(file);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            file.delete();
        }
        randCode = randCode.replaceAll("\\s", "");
        return randCode;
    }

    private static String read(File imageFile) throws Exception {
        String result = "";
        BufferedImage buffImg = ImageIO.read(imageFile);
        if(buffImg == null) {
        	//System.out.println("tk.mystudio.ocr.OCR 60 line occur problem...");
        	return "";
        }
        //logPointRange(buffImg);
        greyImage(buffImg);
        //logPointRange(buffImg);
        twoValueImage(buffImg);
        //logPointRange(buffImg);
        cleanMixLineImage(buffImg);
        //logPointRange(buffImg);
        cleanAlongPointImage(buffImg);
        //logPointRange(buffImg);
        cleanStrangePointImage(buffImg);
		//logPointRange(buffImg);
		int width = buffImg.getWidth();
		int height = buffImg.getHeight();
		//buffImg = resize(buffImg, (int) (width * 1.5), (int) (height * 1.5));
        //logPointRange(buffImg);
        //BufferedImage newbuffImg = createEmptyImage(buffImg, buffImg.getWidth() * 2, buffImg.getHeight());
        //splitAndFixImgToNew(buffImg, newbuffImg);
        BufferedImage newcleanbuffImg = createEmptyImage(buffImg, buffImg.getWidth() * 2, buffImg.getHeight());
        splitAndCleanImgToNew(buffImg, newcleanbuffImg);
        BufferedImage newcenterbuffImg = createEmptyImage(buffImg, buffImg.getWidth() * 2, buffImg.getHeight());
        splitAndCenterImgToNew(newcleanbuffImg, newcenterbuffImg);
        //logPointRange(newcenterbuffImg);

        boolean status = ImageIO.write(newcenterbuffImg, IMAGE_EXT, new File(imageFile.getParentFile(), imageFile.getName() + "." + IMAGE_EXT));

        File outputFile = new File(imageFile.getParentFile(), "output");
        StringBuffer strB = new StringBuffer();

        List<String> cmd = new ArrayList<String>();
        cmd.add(tessPath + "\\tesseract");
        cmd.add("");
        cmd.add(outputFile.getName());
        cmd.add(LANG_OPTION);
        // cmd.add("eng");
        cmd.add("randcode");
        // cmd.add("nobatch");
        // cmd.add("digitsandalphabets");

        ProcessBuilder pb = new ProcessBuilder(new String[0]);
        pb.directory(imageFile.getParentFile());
        if (status)
            cmd.set(1, imageFile.getName() + "." + IMAGE_EXT);
        else
            cmd.set(1, imageFile.getName());
        pb.command(cmd);
        pb.redirectErrorStream(true);
        Process process = pb.start();
        int w = process.waitFor();
        if (w == 0) {
            BufferedReader in = new BufferedReader(new InputStreamReader(new FileInputStream(outputFile.getAbsolutePath() + ".txt"), "UTF-8"));
            String str;
            while ((str = in.readLine()) != null) {
                strB.append(str).append(EOL);
            }
            in.close();
        } else {
            String msg;
            switch (w) {
            case 1:
                msg = "Errors accessing files. There may be spaces in your image's filename.";
                break;
            case 29:
                msg = "Cannot recognize the image or its selected region.";
                break;
            case 31:
                msg = "Unsupported image format.";
                break;
            default:
                msg = "Errors occurred.";
            }
            throw new RuntimeException(msg);
		}
		new File(outputFile.getAbsolutePath() + ".txt").delete();
		new File(imageFile.getAbsolutePath() + "." + IMAGE_EXT).delete();
		result = strB.toString().trim();
		return result;
	}
    
	/**
     * 将现有图层拷贝到新图层，并且将不连续的点图分离开来，居中显示
     * @param targetW 目标长
     * @param targetH 目标宽
     * @return
     */
	private static void splitAndFixImgToNew(BufferedImage buffImg, BufferedImage newbuffImg) {
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
		int lastnewX = 0;
		boolean flag = true;
		Map<Integer, Map<String, Object>> cacheStrangeContinueTempBuffImgMap = new LinkedHashMap<Integer, Map<String, Object>>();
		int i=0;
		while (flag) {
			Boundary bound = new Boundary();
			BufferedImage tempbuffImg = createEmptyImage(buffImg, buffImg.getWidth(), buffImg.getHeight());
			for (int x = minX; x < width; x++) {
				if (bound.minX != -1) {
					break;
				}
				for (int y = minY; y < height; y++) {
					int rgb = buffImg.getRGB(x, y);
					Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
					if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
						bound.minX = bound.maxX = x;
						bound.minY = bound.maxY = y;
						break;
					}
				}
			}
			if (bound.minX == -1) {
				flag = false;
			} else {
				boolean isrelateToNext = cutContinuousPointToTempImg(bound.minX, bound.minY, buffImg, tempbuffImg, bound);
				Map<String, Object> itemmap = new HashMap<String, Object>();
				itemmap.put("bound", bound);
				itemmap.put("tempbuffImg", tempbuffImg);
				itemmap.put("isrelateToNext", isrelateToNext);
				cacheStrangeContinueTempBuffImgMap.put(i++, itemmap);
				/*if ((bound.maxX - bound.minX > 3 && bound.maxY - bound.minY > 2) || (bound.maxX - bound.minX > 2 && bound.maxY - bound.minY > 3)) {
					//lastnewX = copyTempImgToNewbuffImg(tempbuffImg, newbuffImg, bound, lastnewX);
				} else {
					//logPointRange(tempbuffImg);
				}*/
			}
		}
		Entry<Integer, Map<String, Object>> lastEntry = null;
		for(Iterator<Entry<Integer, Map<String, Object>>> iter = cacheStrangeContinueTempBuffImgMap.entrySet().iterator(); iter.hasNext();) {
			Entry<Integer, Map<String, Object>> entry = iter.next();
			BufferedImage tempbuffImg = (BufferedImage) entry.getValue().get("tempbuffImg");
			
			//logPointRange(tempbuffImg);
			
			Boundary bound = (Boundary) entry.getValue().get("bound");
			boolean isrelateToNext = (Boolean) entry.getValue().get("isrelateToNext");
			if(iter.hasNext()) {
				isrelateToNext = true;
			} else {
				isrelateToNext = false;
			}
			boolean isrelateToLast = false;
			if (lastEntry != null) {
				isrelateToLast = (Boolean) lastEntry.getValue().get("isrelateToNext");
				BufferedImage lastTempbuffImg = (BufferedImage) lastEntry.getValue().get("tempbuffImg");
				Boundary lastBound = (Boundary) lastEntry.getValue().get("bound");
				if(!isrelateToLast && (lastBound.maxX - bound.minX >= -1 && (lastBound.maxY - bound.minY >= -1 || bound.maxY - lastBound.minY >= -1))) {
					isrelateToLast = true;
				}
				if (isrelateToLast && ((lastBound.maxX - bound.minX > 0 && (bound.maxX - bound.minX < 12 || (bound.maxY - bound.minY < 12 && Math.abs((bound.maxY + bound.minY)/2 - (lastBound.maxY + lastBound.minY)/2) < 2))) || (bound.maxX - bound.minX < 10 && bound.maxY - bound.minY < 12))) {
						copyTempImgToNewbuffImg(lastTempbuffImg, tempbuffImg);
						bound.maxX = (bound.maxX > lastBound.maxX) ? bound.maxX : lastBound.maxX;
						bound.maxY = (bound.maxY > lastBound.maxY) ? bound.maxY : lastBound.maxY;
						bound.minX = (bound.minX < lastBound.minX) ? bound.minX : lastBound.minX;
						bound.minY = (bound.minY < lastBound.minY) ? bound.minY : lastBound.minY;
				} else {
					//if(lastBound.maxX - lastBound.minX > 8 || lastBound.maxY - lastBound.minY > 8) {
						//if((lastBound.maxX - lastBound.minX > 3 && lastBound.maxY - lastBound.minY > 2) || (lastBound.maxX - lastBound.minX > 2 && lastBound.maxY - lastBound.minY > 3)) {
							lastnewX = copyTempImgToNewbuffImg(lastTempbuffImg, newbuffImg, lastBound, lastnewX);
						//}
					//}
				}
			}
			if (!isrelateToNext) {
				//if(bound.maxX - bound.minX > 8 || bound.maxY - bound.minY > 8) {
					//if((bound.maxX - bound.minX > 3 && bound.maxY - bound.minY > 2) || (bound.maxX - bound.minX > 2 && bound.maxY - bound.minY > 3)) {
						lastnewX = copyTempImgToNewbuffImg(tempbuffImg, newbuffImg, bound, lastnewX);
					//}
				//}
				lastEntry = null;
			} else {
				lastEntry = entry;
			}
		}
	}
    
	/**
     * 将现有图层拷贝到新图层，并且将不连续的点图分离开来，居中显示
     * @param targetW 目标长
     * @param targetH 目标宽
     * @return
     */
	private static void splitAndCenterImgToNew(BufferedImage buffImg, BufferedImage newbuffImg) {
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
		int lastnewX = 0;
		boolean flag = true;
		int i=0;
		while (flag) {
			Boundary bound = new Boundary();
			BufferedImage tempbuffImg = createEmptyImage(buffImg, buffImg.getWidth(), buffImg.getHeight());
			for (int x = minX; x < width; x++) {
				if (bound.minX != -1) {
					break;
				}
				for (int y = minY; y < height; y++) {
					int rgb = buffImg.getRGB(x, y);
					Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
					if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
						bound.minX = bound.maxX = x;
						bound.minY = bound.maxY = y;
						break;
					}
				}
			}
			if (bound.minX == -1) {
				flag = false;
			} else {
				cutContinuousCleanPointToTempImg(bound.minX, bound.minY, buffImg, tempbuffImg, bound);
				if(bound.maxX - bound.minX > 4 || bound.maxY - bound.minY > 4) {
					if ((bound.maxX - bound.minX > 3 && bound.maxY - bound.minY > 2) || (bound.maxX - bound.minX > 2 && bound.maxY - bound.minY > 3)) {
						lastnewX = copyTempImgToNewbuffImg(tempbuffImg, newbuffImg, bound, lastnewX);
					} else {
						//logPointRange(tempbuffImg);
					}
				}
			}
		}
	}
    
	private static int copyTempImgToNewbuffImg(BufferedImage tempbuffImg, BufferedImage newbuffImg, Boundary bound, int lastnewX) {
		int newheight = newbuffImg.getHeight();
		int newwidth = newbuffImg.getWidth();
		int newminY = 0;
		int newminX = 0;
		Boundary newbound = new Boundary();
		newbound.minX = lastnewX + 3;
		newbound.minY = 0;
		newbound.maxX = newbound.minX + bound.maxX - bound.minX + 4;
		int initnewx = newbound.minX;
		int initnewy = (newheight - bound.maxY + bound.minY) / 2;
		int inittempx = bound.minX;
		int inittempy = bound.minY;
		for (int y = 0; y < bound.maxY - bound.minY + 1; y++) {
			for (int x = 0; x < bound.maxX - bound.minX + 1; x++) {
				int rgb = tempbuffImg.getRGB(inittempx + x, inittempy + y);
				Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
					newbuffImg.setRGB(initnewx + x, initnewy + y, rgb);
				}
			}
		}
		return newbound.maxX;
	}
    
	/**
     * 将现有图层拷贝到新图层，并且将不连续的点图分离开来，然后去掉小黑点
     * @param targetW 目标长
     * @param targetH 目标宽
     * @return
     */
	private static void splitAndCleanImgToNew(BufferedImage buffImg, BufferedImage newbuffImg) {
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
		boolean flag = true;
		while (flag) {
			Boundary bound = new Boundary();
			BufferedImage tempbuffImg = createEmptyImage(buffImg, buffImg.getWidth(), buffImg.getHeight());
			for (int x = minX; x < width; x++) {
				if (bound.minX != -1) {
					break;
				}
				for (int y = minY; y < height; y++) {
					int rgb = buffImg.getRGB(x, y);
					Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
					if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
						bound.minX = bound.maxX = x;
						bound.minY = bound.maxY = y;
						break;
					}
				}
			}
			if (bound.minX == -1) {
				flag = false;
			} else {
				cutContinuousCleanPointToTempImg(bound.minX, bound.minY, buffImg, tempbuffImg, bound);
				if(bound.maxX - bound.minX > 4 || bound.maxY - bound.minY > 4) {
					if ((bound.maxX - bound.minX > 3 && bound.maxY - bound.minY > 2) || (bound.maxX - bound.minX > 2 && bound.maxY - bound.minY > 3)) {
						copyTempImgToNewbuffImg(tempbuffImg, newbuffImg);
					} else {
						//logPointRange(tempbuffImg);
					}
				}
			}
		}
	}
    
	private static void copyTempImgToNewbuffImg(BufferedImage tempbuffImg, BufferedImage newbuffImg) {
		int height = tempbuffImg.getHeight();
		int width = tempbuffImg.getWidth();
		int minY = 0;
		int minX = 0;
		for (int y = minY; y < height; y++) {
			for (int x = minX; x < width; x++) {
				int rgb = tempbuffImg.getRGB(x, y);
				Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
					newbuffImg.setRGB(x, y, rgb);
				}
			}
		}
	}
    
	/**
	 * 计算两张图片的距离
	 * @param buffImg1
	 * @param buffImg2
	 * @return
	 */
	private static int calTwoBuffImgDistance(BufferedImage buffImg1, BufferedImage buffImg2) {
		int height1 = buffImg1.getHeight();
		int width1 = buffImg1.getWidth();
		int height2 = buffImg2.getHeight();
		int width2 = buffImg2.getWidth();
		int minY = 0;
		int minX = 0;
		int length = 10;
		for (int y1 = minY; y1 < height1; y1++) {
			for (int x1 = minX; x1 < width1; x1++) {
				int rgb1 = buffImg1.getRGB(x1, y1);
				Color color1 = new Color(rgb1); // 根据rgb的int值分别取得r,g,b颜色。
				if (color1.getRed() < 126 || color1.getGreen() < 126 || color1.getBlue() < 126) {
					for (int y2 = minY; y2 < height2; y2++) {
						for (int x2 = minX; x2 < width2; x2++) {
							int rgb2 = buffImg2.getRGB(x2, y2);
							Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
							if (color2.getRed() < 126 || color2.getGreen() < 126 || color2.getBlue() < 126) {
								int tempminlen = 100;
								if(x1 == x2) {
									tempminlen = Math.abs(y2-y1);
								}
								if(y1 == y2) {
									tempminlen = Math.abs(x2-x1);
								}
								if(tempminlen == 100) {
									tempminlen = (int) Math.sqrt((y2 - y1) * (y2 - y1) + (x2 - x1) * (x2 - x1));
								}
								if(tempminlen < length) {
									length = tempminlen;
								}
							}
						}
					}
				}
			}
		}
		return length;
	}

	/**
	 * 剪切连续点到临时图层
	 * @param x
	 * @param y
	 * @param buffImg
	 * @param tempbuffImg
	 * @param bound
	 */
	private static boolean cutContinuousPointToTempImg(int x, int y,
			BufferedImage buffImg, BufferedImage tempbuffImg, Boundary bound) {
		boolean isrelateToNext = false;
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
        Color black = new Color(0, 0, 0);
        Color blank = new Color(255, 255, 255);
        buffImg.setRGB(x, y, blank.getRGB());
        tempbuffImg.setRGB(x, y, black.getRGB());
		if (x < bound.minX) {
			bound.minX = x;
		}
		if (y < bound.minY) {
			bound.minY = y;
		}
		if (x > bound.maxX) {
			bound.maxX = x;
		}
		if (y > bound.maxY) {
			bound.maxY = y;
		}
		// 直
		if (y > minY) {
			int rgbt = buffImg.getRGB(x, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = cutContinuousPointToTempImg(x, y - 1, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		if (x > minX) {
			int rgbt = buffImg.getRGB(x - 1, y);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = cutContinuousPointToTempImg(x - 1, y, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		if (y < height - 1) {
			int rgbt = buffImg.getRGB(x, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = cutContinuousPointToTempImg(x, y + 1, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		if (x < width - 1) {
			int rgbt = buffImg.getRGB(x + 1, y);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = cutContinuousPointToTempImg(x + 1, y, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		// 斜
		if (x > minX && y > minY) {
			int rgbt = buffImg.getRGB(x - 1, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				//isrelateToNext = true;
				isrelateToNext = cutContinuousPointToTempImg(x - 1, y - 1, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		if (x > minX && y < height - 1) {
			int rgbt = buffImg.getRGB(x - 1, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				//isrelateToNext = true;
				isrelateToNext = cutContinuousPointToTempImg(x - 1, y + 1, buffImg, tempbuffImg, bound) || isrelateToNext;
			}
		}
		if (x < width - 1 && y < height - 1) {
			int rgbt = buffImg.getRGB(x + 1, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = true;
				//cutContinuousPointToTempImg(x + 1, y + 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x < width - 1 && y > minY) {
			int rgbt = buffImg.getRGB(x + 1, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				isrelateToNext = true;
				//cutContinuousPointToTempImg(x + 1, y - 1, buffImg, tempbuffImg, bound);
			}
		}
		return isrelateToNext;
	}

	/**
	 * 剪切连续点到临时图层
	 * @param x
	 * @param y
	 * @param buffImg
	 * @param tempbuffImg
	 * @param bound
	 */
	private static void cutContinuousCleanPointToTempImg(int x, int y,
			BufferedImage buffImg, BufferedImage tempbuffImg, Boundary bound) {
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
        Color black = new Color(0, 0, 0);
        Color blank = new Color(255, 255, 255);
        buffImg.setRGB(x, y, blank.getRGB());
        tempbuffImg.setRGB(x, y, black.getRGB());
		if (x < bound.minX) {
			bound.minX = x;
		}
		if (y < bound.minY) {
			bound.minY = y;
		}
		if (x > bound.maxX) {
			bound.maxX = x;
		}
		if (y > bound.maxY) {
			bound.maxY = y;
		}
		// 直
		if (y > minY) {
			int rgbt = buffImg.getRGB(x, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x, y - 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x > minX) {
			int rgbt = buffImg.getRGB(x - 1, y);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x - 1, y, buffImg, tempbuffImg, bound);
			}
		}
		if (y < height - 1) {
			int rgbt = buffImg.getRGB(x, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x, y + 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x < width - 1) {
			int rgbt = buffImg.getRGB(x + 1, y);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x + 1, y, buffImg, tempbuffImg, bound);
			}
		}
		// 斜
		if (x > minX && y > minY) {
			int rgbt = buffImg.getRGB(x - 1, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x - 1, y - 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x > minX && y < height - 1) {
			int rgbt = buffImg.getRGB(x - 1, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x - 1, y + 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x < width - 1 && y < height - 1) {
			int rgbt = buffImg.getRGB(x + 1, y + 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x + 1, y + 1, buffImg, tempbuffImg, bound);
			}
		}
		if (x < width - 1 && y > minY) {
			int rgbt = buffImg.getRGB(x + 1, y - 1);
			Color color = new Color(rgbt); // 根据rgb的int值分别取得r,g,b颜色。
			if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
				cutContinuousCleanPointToTempImg(x + 1, y - 1, buffImg, tempbuffImg, bound);
			}
		}
	}

	/**
     * 创建一个空白图片
     * @param targetW 目标长
     * @param targetH 目标宽
     * @return
     */
	public static BufferedImage createEmptyImage(BufferedImage source, int targetW, int targetH) {
		BufferedImage target = null;
		int imageType = source.getType();
		if (imageType == BufferedImage.TYPE_CUSTOM) { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
		} else {
			target = new BufferedImage(targetW, targetH, imageType);
		}
		int height = target.getHeight();
		int width = target.getWidth();
		int minY = 0;
		int minX = 0;
        Color newColor = new Color(255, 255, 255);
		for (int y = minY; y < height; y++) {
			for (int x = minX; x < width; x++) {
                target.setRGB(x, y, newColor.getRGB());
			}
		}
		return target;
	}
    
    
    /**
     * 改变图片的大小
     * @param source 源文件
     * @param targetW 目标长
     * @param targetH 目标宽
     * @return
     */
    public static BufferedImage resize(BufferedImage source, int targetW, int targetH) {
        int type = source.getType();
        BufferedImage target = null;
        double sx = (double) targetW / source.getWidth();
        double sy = (double) targetH / source.getHeight();
        // 这里想实现在targetW，targetH范围内实现等比缩放。如果不需要等比缩放
        // 则将下面的if else语句注释即可
        if (sx > sy) {
            sx = sy;
            targetW = (int) (sx * source.getWidth());
        } else {
            sy = sx;
            targetH = (int) (sy * source.getHeight());
        }
        if (type == BufferedImage.TYPE_CUSTOM) { // handmade
            ColorModel cm = source.getColorModel();
            WritableRaster raster = cm.createCompatibleWritableRaster(targetW, targetH);
            boolean alphaPremultiplied = cm.isAlphaPremultiplied();
            target = new BufferedImage(cm, raster, alphaPremultiplied, null);
        } else {
            target = new BufferedImage(targetW, targetH, type);
        }
        Graphics2D g = target.createGraphics();
        // smoother than exlax:
        g.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
        g.drawRenderedImage(source, AffineTransform.getScaleInstance(sx, sy));
        g.dispose();
        return target;
    }
	
    /**
     * 打印点阵
     * @return
     */
	private static void logPointRange(BufferedImage buffImg) {
		// test 打印点阵
		int height = buffImg.getHeight();
		int width = buffImg.getWidth();
		int minY = 0;
		int minX = 0;
		for (int y = minY; y < height; y++) {
			for (int x = minX; x < width; x++) {
				int rgb = buffImg.getRGB(x, y);
				Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
					System.out.print('*');
				} else {
					System.out.print(' ');
				}
			}
			System.out.print('\n');
		}
	}
    
    
    /**
     * 1.灰度化图片
     * @return
     */
	private static void greyImage(BufferedImage buffImg) {
        // 1.首先灰度化，灰度值=0.3R+0.59G+0.11B：
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
                int gray = (int) (0.3 * color.getRed() + 0.59 * color.getGreen() + 0.11 * color.getBlue());
                Color newColor = new Color(gray, gray, gray);
                buffImg.setRGB(x, y, newColor.getRGB());
            }
        }
	}
    
    /**
     * 2.灰度反转图片
     * @deprecated
     * @return
     */
	private static void greyReverseImage(BufferedImage buffImg) {
        // 2.其次是灰度反转：
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
                Color newColor = new Color(255 - color.getRed(), 255 - color.getGreen(), 255 - color.getBlue());
                buffImg.setRGB(x, y, newColor.getRGB());
            }
        }
	}
    
    /**
     * 3.二值化图片
     * @return
     */
	private static void twoValueImage(BufferedImage buffImg) {
        // 3.再次是二值化，取图片的平均灰度作为阈值，低于该值的全都为0，高于该值的全都为255：
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        Color black = new Color(0, 0, 0);
        Color blank = new Color(255, 255, 255);
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
                    buffImg.setRGB(x, y, black.getRGB());
                } else {
                    buffImg.setRGB(x, y, blank.getRGB());
                }
            }
        }
	}
    
    /**
     * 4.去干扰线
     * @return
     */
	private static void cleanMixLineImage(BufferedImage buffImg) {
        // 4.再次是去干扰线，上下均没有像素的清除掉
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        // 是否需要继续找下一干扰线的起点
        boolean findFisrtLinePoint = true;
        while(findFisrtLinePoint) {
            // 记下起点坐标
            int interruptFirstLineX = -1;
            int interruptFirstLineY = -1;
            // 记下上一坐标
            int interruptLastLineX = -1;
            int interruptLastLineY = -1;
            // 记下偏移容差(纵向偏量为+(上)-(下)1时,横向的位移)
            int interruptLineFixS = 0;
            boolean findout = false;// 记录是否找到偏移容差
	        for (int x = minX; x < width; x++) {
				if (interruptFirstLineX == -1 && interruptFirstLineY == -1 && x > 3) {
					findFisrtLinePoint = false;
					break;
				}
	        	for (int y = minY; y < height; y++) {
					if (interruptFirstLineX != -1 && interruptFirstLineY != -1) {
						if(findout) {
							if (interruptLineFixS > 0) {
								if ((y < interruptLastLineY)
										|| (y - interruptLastLineY) > (x
												- interruptLastLineX + 1)
												/ (interruptLineFixS * 1.0)) {
									continue;
								}
							} else if (interruptLineFixS < 0) {
								if ((y > interruptLastLineY)
										|| (interruptLastLineY - y) > (x
												- interruptLastLineX + 1)
												/ (-interruptLineFixS * 1.0)) {
									continue;
								}
							}
						} else {
							if (Math.abs(y - interruptLastLineY) > 1) {
								continue;
							}
						}
					}
					int rgb = buffImg.getRGB(x, y);
					Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
					if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
						boolean flg = true; // 是否可以去掉该像素
						// 获取上一像素
						if (y > minY) {
							int rgb2 = buffImg.getRGB(x, y - 1);
							Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
							if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
								flg = false;
							}
						}
						if (flg && y < height - 1) {
							int rgb2 = buffImg.getRGB(x, y + 1);
							Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
							if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
								flg = false;
							}
						}
						if (flg && x > minX && x < width - 1 && y > minY
								&& y < height - 1) {
							int rgb21 = buffImg.getRGB(x + 1, y + 1);
							int rgb22 = buffImg.getRGB(x - 1, y - 1);
							Color color21 = new Color(rgb21); // 根据rgb的int值分别取得r,g,b颜色。
							Color color22 = new Color(rgb22); // 根据rgb的int值分别取得r,g,b颜色。
							if ((color21.getRed() < 122 || color21.getGreen() < 122 || color21.getBlue() < 122) && (color22.getRed() < 122 || color22.getGreen() < 122 || color22.getBlue() < 122)) {
								flg = false;
							}
							int rgb23 = buffImg.getRGB(x - 1, y + 1);
							int rgb24 = buffImg.getRGB(x + 1, y - 1);
							Color color23 = new Color(rgb23); // 根据rgb的int值分别取得r,g,b颜色。
							Color color24 = new Color(rgb24); // 根据rgb的int值分别取得r,g,b颜色。
							if ((color23.getRed() < 122 || color23.getGreen() < 122 || color23.getBlue() < 122) && (color24.getRed() < 122 || color24.getGreen() < 122 || color24.getBlue() < 122)) {
								flg = false;
							}
						}
						if (flg) {
							if (interruptFirstLineX == -1 && interruptFirstLineY == -1) {
								interruptFirstLineX = x;
								interruptFirstLineY = y;
							}
							interruptLastLineX = x;
							interruptLastLineY = y;
							if (!findout) {
								if (interruptFirstLineY == interruptLastLineY) {
									interruptLineFixS++;
								} else {
									interruptLineFixS++;
									interruptLineFixS = interruptLineFixS * (y > interruptFirstLineY ? 1 : -1);
									findout = true;
								}
							}
							Color newColor = new Color(255, 255, 255);
							buffImg.setRGB(x, y, newColor.getRGB());
							break;
						}
					}
	            }
	        }
        }
	}
    
    /**
     * 5.去噪点
     * @return
     */
	private static void cleanAlongPointImage(BufferedImage buffImg) {
		// 5.去噪点,把有三面以上空白的点给去掉(像素直径径小于1的给去掉)
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
					boolean flg = true; // 是否可以去掉该像素
					boolean flgr = true; // 是否可以去掉该像素
					// 获取周边像素
					int side = 0;
					if (y > minY) {
						int rgb2 = buffImg.getRGB(x, y - 1);
		                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
		                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flg = false;
						}
					}
					if (y < height - 1) {
						int rgb2 = buffImg.getRGB(x, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flg = false;
						}
					}
					if (x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flg = false;
						}
					}
					if (x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flg = false;
						}
					}
					if (y > minY && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flgr = false;
						} else {
							side++;
						}
					}
					if (y < height - 1 && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flgr = false;
						} else {
							side++;
						}
					}
					if (y > minY && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flgr = false;
						} else {
							side++;
						}
					}
					if (y < height - 1 && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							flgr = false;
						} else {
							side++;
						}
					}
					if (flg && (flgr || side >= 3)) {
						Color newColor = new Color(255, 255, 255);
						buffImg.setRGB(x, y, newColor.getRGB());
					}
                }
            }
        }
	}
    
    /**
     * 5.1去奇异点
     * @return
     */
	private static void cleanStrangePointImage(BufferedImage buffImg) {
		// 5.1去奇异点,去掉带有突出直角形的点
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                Color color = new Color(rgb); // 根据rgb的int值分别取得r,g,b颜色。
				if (color.getRed() < 122 || color.getGreen() < 122 || color.getBlue() < 122) {
					// 获取某一方向像素
					boolean sidee = false;
					boolean sides = false;
					boolean sidew = false;
					boolean siden = false;
					boolean sidees = false;
					boolean sidews = false;
					boolean sideen = false;
					boolean sidewn = false;
					boolean sideei = false;
					boolean sidesi = false;
					boolean sidewi = false;
					boolean sideni = false;
					boolean sideesi = false;
					boolean sidewsi = false;
					boolean sideeni = false;
					boolean sidewni = false;
					boolean sideel = false;
					boolean sideer = false;
					boolean sidesl = false;
					boolean sidesr = false;
					boolean sidewl = false;
					boolean sidewr = false;
					boolean sidenl = false;
					boolean sidenr = false;
					
					// 邻
					if (y > minY) {
						int rgb2 = buffImg.getRGB(x, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							siden = true;
						}
					} else {
						siden = true;
					}
					if (y < height - 1) {
						int rgb2 = buffImg.getRGB(x, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sides = true;
						}
					} else {
						sides = true;
					}
					if (x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidew = true;
						}
					} else {
						sidew = true;
					}
					if (x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidee = true;
						}
					} else {
						sidee = true;
					}
					
					// 间邻
					if (y > minY + 1) {
						int rgb2 = buffImg.getRGB(x, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideni = true;
						}
					}
					if (y < height - 2) {
						int rgb2 = buffImg.getRGB(x, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidesi = true;
						}
					}
					if (x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewi = true;
						}
					}
					if (x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideei = true;
						}
					}
					
					// 斜邻
					if (y > minY && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewn = true;
						}
					} else {
						sidewn = true;
					}
					if (y < height - 1 && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidews = true;
						}
					} else {
						sidews = true;
					}
					if (y > minY && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideen = true;
						}
					} else {
						sideen = true;
					}
					if (y < height - 1 && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidees = true;
						}
					} else {
						sidees = true;
					}
					
					// 斜正隔
					if (y > minY + 1 && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewni = true;
						}
					}
					if (y < height - 2 && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewsi = true;
						}
					}
					if (y > minY + 1 && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideeni = true;
						}
					}
					if (y < height - 2 && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideesi = true;
						}
					}
					
					// 斜斜隔
					if (y > minY && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewr = true;
						}
					}
					if (y > minY + 1 && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidenl = true;
						}
					}
					if (y < height - 1 && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewl = true;
						}
					}
					if (y < height - 2 && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidesr = true;
						}
					}
					if (y > minY + 1 && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidenr = true;
						}
					}
					if (y > minY && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideel = true;
						}
					}
					if (y < height - 1 && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideer = true;
						}
					}
					if (y < height - 2 && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidesl = true;
						}
					}
					
					Color blankColor = new Color(255, 255, 255);
					// 西西南
					if(sidewi && !sidew 
							&& sidewl && sidews && sides 
							&& !sidewr && !sidewn && !siden 
							&& !sidewni && !sidenl && !sideni 
							&& !sideeni && !sidenr 
							&& !sideen && !sideel 
							&& !sidee && !sideei 
							&& !sidees && !sideer
							&& !sidesl && !sideesi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 西西北
					if(sidewi && !sidew 
							&& sidewr && sidewn && siden 
							&& !sidewl && !sidews && !sides 
							&& !sidewsi && !sidesr && !sidesi 
							&& !sideeni && !sidenr 
							&& !sideen && !sideel 
							&& !sidee && !sideei 
							&& !sidees && !sideer
							&& !sidesl && !sideesi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 东东北
					if(sideei && !sidee 
							&& sideel && sideen && siden 
							&& !sideer && !sidees && !sides 
							&& !sideesi && !sidesl && !sidesi 
							&& !sidewsi && !sidesr 
							&& !sidews && !sidewl 
							&& !sidew && !sidewi 
							&& !sidewn && !sidewr
							&& !sidenl && !sidewni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 东东南
					if(sideei && !sidee 
							&& sideer && sidees && sides 
							&& !sideel && !sideen && !siden 
							&& !sideeni && !sidenr && !sideni 
							&& !sidewsi && !sidesr 
							&& !sidews && !sidewl 
							&& !sidew && !sidewi 
							&& !sidewn && !sidewr
							&& !sidenl && !sidewni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 北西北
					if(sideni && !siden 
							&& sidenl && sidewn && sidew 
							&& !sidenr && !sideen && !sidee 
							&& !sideeni && !sideel && !sideei 
							&& !sideesi && !sideer 
							&& !sidees && !sidesl 
							&& !sides && !sidesi 
							&& !sidews && !sidesr
							&& !sidewl && !sidewsi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 北东北
					if(sideni && !siden 
							&& sidenr && sideen && sidee 
							&& !sidenl && !sidewn && !sidew 
							&& !sidewni && !sidewr && !sidewi 
							&& !sideesi && !sideer 
							&& !sidees && !sidesl 
							&& !sides && !sidesi 
							&& !sidews && !sidesr
							&& !sidewl && !sidewsi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 南东南
					if(sidesi && !sides 
							&& sidesl && sidees && sidee 
							&& !sidesr && !sidews && !sidew 
							&& !sidewsi && !sidewl && !sidewi 
							&& !sidewni && !sidewr 
							&& !sidewn && !sidenl 
							&& !siden && !sideni 
							&& !sideen && !sidenr
							&& !sideel && !sideeni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 南西南
					if(sidesi && !sides 
							&& sidesr && sidews && sidew 
							&& !sidesl && !sidees && !sidee 
							&& !sideesi && !sideer && !sideei 
							&& !sidewni && !sidewr 
							&& !sidewn && !sidenl 
							&& !siden && !sideni 
							&& !sideen && !sidenr
							&& !sideel && !sideeni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
						
					// 西北
					if (sidewn && sidew && siden && !sidee && !sides && !sidews && !sideen && !sidees && (sidewl || sidenr) && !sidewni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 西南
					if (sidews && sidew && sides && !sidee && !siden && !sidewn && !sideen && !sidees && (sidewr || sidesl) && !sidewsi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 东北
					if (sideen && sidee && siden && !sidew && !sides && !sidees && !sidewn && !sidews && (sideer || sidenl) && !sideeni) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
					// 东南
					if (sidees && sidee && sides && !sidew && !siden && !sideen && !sideen && !sidews && (sideel || sidesr) && !sideesi) {
						buffImg.setRGB(x, y, blankColor.getRGB());
					}
                }
            }
        }
	}
    
    /**
     * 6.修复空隙
     * @deprecated
     * @return
     */
	// TODO
	private static void fixedMissedPointImage(BufferedImage buffImg) {
		// 6.修复空隙(检查四个朝向:东南、西南、东北、西北)
        int height = buffImg.getHeight();
        int width = buffImg.getWidth();
        int minY = 0;
        int minX = 0;
        for (int y = minY; y < height; y++) {
            for (int x = minX; x < width; x++) {
                int rgb = buffImg.getRGB(x, y);
                if (rgb != -1) {
					boolean flges = false; // 东南是否要修复
					boolean flgws = false; // 西南是否要修复
					boolean flgen = false; // 东北是否要修复
					boolean flgwn = false; // 西北是否要修复
					// 获取某一方向像素
					boolean sidee = false;
					boolean sides = false;
					boolean sidew = false;
					boolean siden = false;
					boolean sidees = false;
					boolean sidews = false;
					boolean sideen = false;
					boolean sidewn = false;
					boolean sideesi = false;
					boolean sidewsi = false;
					boolean sideeni = false;
					boolean sidewni = false;
					
					// 邻
					if (y > minY) {
						int rgb2 = buffImg.getRGB(x, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							siden = true;
						}
					} else {
						siden = true;
					}
					if (y < height - 1) {
						int rgb2 = buffImg.getRGB(x, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sides = true;
						}
					} else {
						sides = true;
					}
					if (x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidew = true;
						}
					} else {
						sidew = true;
					}
					if (x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidee = true;
						}
					} else {
						sidee = true;
					}
					
					// 斜邻
					if (y > minY && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewn = true;
						}
					} else {
						sidewn = true;
					}
					if (y < height - 1 && x > minX) {
						int rgb2 = buffImg.getRGB(x - 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidews = true;
						}
					} else {
						sidews = true;
					}
					if (y > minY && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y - 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideen = true;
						}
					} else {
						sideen = true;
					}
					if (y < height - 1 && x < width - 1) {
						int rgb2 = buffImg.getRGB(x + 1, y + 1);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidees = true;
						}
					} else {
						sidees = true;
					}
					
					// 斜正隔
					if (y > minY + 1 && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewni = true;
						}
					}
					if (y < height - 2 && x > minX + 1) {
						int rgb2 = buffImg.getRGB(x - 2, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sidewsi = true;
						}
					}
					if (y > minY + 1 && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y - 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideeni = true;
						}
					}
					if (y < height - 2 && x < width - 2) {
						int rgb2 = buffImg.getRGB(x + 2, y + 2);
	                	Color color2 = new Color(rgb2); // 根据rgb的int值分别取得r,g,b颜色。
	                	if (color2.getRed() < 122 || color2.getGreen() < 122 || color2.getBlue() < 122) {
							sideesi = true;
						}
					}
					Color newColor = new Color(0, 0, 0);
					// 获取临近像素
					if (!sidewn && !sidew && !siden && sidewni) {
						flgwn = true;
						buffImg.setRGB(x - 1, y - 1, newColor.getRGB());
					}
					if (!sideen && !sidee && !siden && sideeni) {
						flgen = true;
						buffImg.setRGB(x + 1, y - 1, newColor.getRGB());
					}
					if (!sidews && !sidew && !sides && sidewsi) {
						flgws = true;
						buffImg.setRGB(x - 1, y + 1, newColor.getRGB());
					}
					if (!sidees && !sidee && !sides && sideesi) {
						flges = true;
						buffImg.setRGB(x + 1, y + 1, newColor.getRGB());
					}
                }
            }
        }
	}
	
	private static class Boundary {
		int minX = -1;
		int minY = -1;
		int maxX = -1;
		int maxY = -1;
	}

	/*public static void main(String[] args) {
		// Color newColor = new Color(0, 0, 0);
		// System.out.println(newColor.getRGB());
		// newColor = new Color(255, 255, 255);
		// System.out.println(newColor.getRGB());
	}*/

}
